package org.example.src.controller;

import org.example.src.dto.response.ApiResponse;
import org.example.src.service.ApplicationService;
import org.example.src.service.CompanyService;
import org.example.src.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/statistics")
public class StatisticsController extends BaseController {

    @Autowired
    private ApplicationService applicationService;

    @Autowired
    private StudentService studentService;

    @Autowired
    private CompanyService companyService;

    /**
     * 获取实习统计数据
     */
    @GetMapping("/internship")
    public ResponseEntity<ApiResponse<Map<String, Object>>> getInternshipStatistics() {
        Map<String, Object> statistics = new HashMap<>();

        // 获取学生总数
        long totalStudents = studentService.findAllStudents().size();
        statistics.put("totalStudents", totalStudents);

        // 获取企业总数
        long totalCompanies = companyService.findAllCompanies().size();
        statistics.put("totalCompanies", totalCompanies);

        // 获取岗位数量
        long totalJobPosts = applicationService.findAllApplications().size();
        statistics.put("totalApplications", totalJobPosts);

        // 获取通过申请数量
        long approvedApplications = applicationService.findApplicationsByCompanyId(null).stream()
                .filter(app -> "APPROVED".equals(app.getStatus().name())).count();
        statistics.put("approvedApplications", approvedApplications);

        // 计算实习率
        double internshipRate = totalStudents > 0 ? (double) approvedApplications / totalStudents : 0;
        statistics.put("internshipRate", String.format("%.2f%%", internshipRate * 100));

        return success("获取统计数据成功", statistics);
    }

    /**
     * 获取企业统计数据
     */
    @GetMapping("/companies")
    public ResponseEntity<ApiResponse<Map<String, Object>>> getCompanyStatistics() {
        Map<String, Object> statistics = new HashMap<>();

        // 获取已认证企业数
        long approvedCompanies = companyService.findCompaniesByApprovalStatus(true).size();
        statistics.put("approvedCompanies", approvedCompanies);

        // 获取未认证企业数
        long pendingCompanies = companyService.findCompaniesByApprovalStatus(false).size();
        statistics.put("pendingCompanies", pendingCompanies);

        return success("获取企业统计数据成功", statistics);
    }
}
